package com.tianqu.tms.www.member.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Member;
import com.tianqu.tms.entity.User;
import com.tianqu.tms.www.member.controller.MemberCreationResultController;
import com.tianqu.tms.www.member.viewhelper.MemberInputViewHelper;
/**
 * 完成商户信息存储Action
 * 
 * @author tianshifu
 *
 */
public class MemberCreationResultAction extends TmsAction {

	private Logger logger = Logger.getLogger(MemberCreationResultAction.class);
	
	private static final String SYSPATH = "sysPath";
	private static final String OFF="off";
	
	private static final String LEFTLINE ="/";
	
	private static final String TMSPATH = "/tmsweb";
	private static final String BACKFLAG = "backFlag";
	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		
		//isToken(mapping, request);
		MemberInputViewHelper viewhelp = (MemberInputViewHelper)uc.getAttribute(TmsConstants.VIEWHELP);
		request.setAttribute(TmsConstants.VIEWHELP, viewhelp);
		Member member = Member.getInstance();
		//判断商户名称是否重复
		boolean result1 = member.isRepeatMemberName(viewhelp.getRealName(),1);
		if(result1 == true){
			setErrorsToRequest(request, TmsConstants.U4001 ,null);
			return mapping.getInputForward();
		}
		//判断商户短名是否重复
		boolean result2 = member.isRepeatMemberName(viewhelp.getShortName(),2);
		if(result2 == true){
			setErrorsToRequest(request, TmsConstants.U4002 ,null);
			return mapping.getInputForward();
		}
		//判断用户是否重复
		User user = User.getInstance();
		boolean result = user.isRepeatUserId(viewhelp.getUserId());
		if(result == true){
			setErrorsToRequest(request, TmsConstants.U4003 ,null);
			return mapping.getInputForward();
		}
		
		MemberCreationResultController controller = new MemberCreationResultController();
		String sysPath = request.getSession().getServletContext().getRealPath(LEFTLINE).replace(TMSPATH, TmsConstants.BLANK);
		uc.setAttribute(SYSPATH, sysPath);
		controller.execute(uc);
		uc.setAttribute(BACKFLAG, 1);
		
//		setErrorsToRequest(request, TmsConstants.U13004 ,null);
//		return mapping.getInputForward();
		
		MemberInputViewHelper view = new MemberInputViewHelper();
		request.setAttribute(OFF, "信息保存成功!");
		request.setAttribute(TmsConstants.VIEWHELP, view);
		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(TmsConstants.SUCCESS); 
	}

}
